//
//  VistorView.swift
//  SwiftWeiBo
//
//  Created by wyw on 16/9/16.
//  Copyright © 2016年 Style_Y. All rights reserved.
//

import UIKit

// 代理协议 swift中协议必须遵守NSObjectProtocol协议
protocol VisitorViewDelegate : NSObjectProtocol{
    // 登录回调
    func loginBtnDidClick()
    
    // 注册回调
    func registBtnDidClick()
}

class VisitorView: UIView {

    weak var visitorViewDelegate : VisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 创建控件
    func setupVisitorInfo(isHome:Bool , imageName:String , message:String) {
        iconView.isHidden = !isHome
        messageLabel.text = message
        homeIconView.image = UIImage.init(named: imageName)
        
        if isHome {
            startAnimotion();
        }
    }
    
    /// 动画
    private func startAnimotion() {
        // 1.创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        // 2.设置动画属性
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
          // 该属性默认为true，代表动画只要执行完毕就移除，设置为false代表不移除
        anim.isRemovedOnCompletion = false
        // 3.添加动画到图层
        iconView.layer.add(anim, forKey: nil)
    }
    
    private func setupUI() {        // 1. 添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registButton)
        addSubview(loginButton)
        
        // 2.自动布局
        // 2. 自动布局
        /*
         注意:
         纯代码开发时默认可以直接使用setFrame设置控件位置的
         如果要使用自动布局，需要设置translatesAutoresizingMaskIntoConstraints属性
         一旦加入自动布局系统管理，就不要再设置frame
         */
        // 2.1背景图标
        iconView.xmg_AlignInner(type: XMG_AlignType.center, referView: self, size: nil)
        // 2.2遮罩
        maskIconView.xmg_Fill(self)
        // 2.3小房子
        homeIconView.xmg_AlignInner(type: XMG_AlignType.center, referView: self, size: nil)
        // 2.4消息文字
        messageLabel.xmg_AlignVertical(type: XMG_AlignType.bottomCenter, referView: iconView, size: nil)
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 224))
        // 2.5注册按钮
        registButton.xmg_AlignVertical(type: XMG_AlignType.bottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 35), offset: CGPoint(x: 0, y: 20))
        // 2.6登录按钮
        loginButton.xmg_AlignVertical(type: XMG_AlignType.bottomRight, referView: messageLabel, size: CGSize(width: 100, height: 35), offset: CGPoint(x: 0, y: 20))
    
    }
    
    // MARK: - 按钮点击事件
    func loginBtnClick() {
        visitorViewDelegate?.loginBtnDidClick()
    }
    func registBtnClick() {
        visitorViewDelegate?.registBtnDidClick()
    }
    
    // MARK: - 懒加载
    private lazy var iconView:UIImageView = {
    
        let iconView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_image_smallicon"))
        
        return iconView
    }()
    private lazy var maskIconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return iv
    }()
    private lazy var homeIconView:UIImageView = {
        
        let homeIconView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_image_house"))
        
        return homeIconView
    }()
    private lazy var messageLabel:UILabel = {
        
        let messageLabel = UILabel()
        messageLabel.textColor = UIColor.darkGray
        messageLabel.text = "发电机房垃圾收电费了骄傲的浪费就列举了王建安费啦"
        messageLabel.font = UIFont.systemFont(ofSize: 14)
        messageLabel.numberOfLines = 0
        messageLabel.sizeToFit()
        return messageLabel
    }()
    private lazy var loginButton:UIButton = {
        
        let loginButton = UIButton()
        loginButton.setTitle("登录", for: UIControlState.normal)
        loginButton.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), for: UIControlState.normal)
        loginButton.setTitleColor(UIColor.orange, for: UIControlState.normal)
        loginButton.addTarget(self, action: #selector(VisitorView.loginBtnClick), for: UIControlEvents.touchUpInside)
        return loginButton
    }()
    private lazy var registButton:UIButton = {
        
        let registButton = UIButton()
        registButton.setTitle("注册", for: UIControlState.normal)
        registButton.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), for: UIControlState.normal)
        registButton.setTitleColor(UIColor.orange, for: UIControlState.normal)
        registButton.addTarget(self, action: #selector(VisitorView.registBtnClick), for: UIControlEvents.touchUpInside)
        return registButton
    }()

}
